/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-05-13
 * Time:23:58
 */

/**
 * BM29二叉树中和为某一值的路径(━)
 */

public class hasPathSum {
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    boolean flag = false;
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if (root == null) return false;
        find(root, sum);
        return flag;
    }
    public void find(TreeNode root, int sum) {
        if (root.left != null)
            find(root.left, sum - root.val);
        if (root.right != null)
            find(root.right, sum - root.val);
        if (root.left == null && root.right == null && sum == root.val)
            flag = true;
    }
}
